package com.example.dayday.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dayday.config.StudentListeren;
import com.example.dayday.mapper.CourseMapper;
import com.example.dayday.mapper.SpecializedMapper;
import com.example.dayday.mapper.StudentCourseMapper;
import com.example.dayday.pojo.Specialized;
import com.example.dayday.pojo.Student;
import com.example.dayday.mapper.StudentMapper;
import com.example.dayday.service.SpecializedService;
import com.example.dayday.service.StudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dayday.vo.StudentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.naming.ldap.PagedResultsControl;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Helen
 * @since 2022-09-07
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Autowired
    private SpecializedService specializedService;
@Autowired
private StudentCourseMapper studentCourseMapper;

@Autowired
private SpecializedMapper specializedMapper;
@Autowired
  private CourseMapper courseMapper;

    //分页查询
    @Override
    public IPage selectStudent(int page, int limit, String studentName, Long studentId) {
        Page<Student> studentPage = new Page<>(page, limit);
        LambdaQueryWrapper<Student> wq = new LambdaQueryWrapper<>();
        wq.eq(!StringUtils.isEmpty(studentId), Student::getStudentId, studentId);
        wq.like(!StringUtils.isEmpty(studentName), Student::getStudentName, studentName);
        this.page(studentPage, wq);

        Page<StudentVo> studentVoPage = new Page<>();
        BeanUtils.copyProperties(studentPage, studentVoPage);
        List<Student> records = studentPage.getRecords();
        Set<Long> collect = records.stream().map(item -> {
            return item.getClassId();
        }).collect(Collectors.toSet());

        List<Specialized> specializeds = specializedService.listByIds(collect);

        List<StudentVo> collect1 = studentPage.getRecords().stream().map(item -> {
            StudentVo studentVo = new StudentVo();
            BeanUtils.copyProperties(item, studentVo);
            for (Specialized s : specializeds) {
                if (studentVo.getClassId().equals(s.getClassId())) {
                    studentVo.setSpecialized(s.getSpecialized());
                    break;
                }
            }
            return studentVo;
        }).collect(Collectors.toList());
        studentVoPage.setRecords(collect1);

        return studentVoPage;
    }

    //学生自己修改密码
    @Override
    public boolean updatePassword(String oldPassword, String newPassword, Long studentId) {
        LambdaUpdateWrapper<Student> wq = new LambdaUpdateWrapper<>();
        wq.eq(Student::getStudentId,studentId);
        wq.eq(Student::getPassword,oldPassword);
        wq.set(Student::getPassword,newPassword);
        return update(wq);
    }

    //管理员修改学生密码
    @Override
    public boolean updatePassword(String newPassword, Long studentId) {
        LambdaUpdateWrapper<Student> wq = new LambdaUpdateWrapper<>();
        wq.eq(Student::getStudentId,studentId);
        wq.set(Student::getPassword,newPassword);
        return update(wq);
    }

    // 添加学生
    @Override
    public void saveStudent(StudentVo studentVo) {
        //
        String specialized = studentVo.getSpecialized();
        LambdaQueryWrapper<Specialized> wq = new LambdaQueryWrapper<>();
        wq.eq(!StringUtils.isEmpty(specialized), Specialized::getSpecialized, specialized);
        Specialized one = specializedService.getOne(wq);
        studentVo.setClassId(one.getClassId());
        this.save(studentVo);

    }
//
    //修改学生信息
//    @Override
//    public boolean updateStudent(StudentVo studentVo) {
//        String specialized = studentVo.getSpecialized();
//        LambdaQueryWrapper<Specialized> wq = new LambdaQueryWrapper<>();
//        wq.eq(!StringUtils.isEmpty(specialized),Specialized::getSpecialized,specialized);
//        Specialized one = specializedService.getOne(wq);
//        studentVo.setClassId(one.getClassId());
//        boolean b = this.updateById(studentVo);
//        return b;
//    }

    @Override
    public Student getStudentById(Long id) {
        Student StudentList = this.getById(id);
        StudentVo studentVo = new StudentVo();
        BeanUtils.copyProperties(StudentList,studentVo);
        Long classId = studentVo.getClassId();
        LambdaQueryWrapper<Specialized> wq = new LambdaQueryWrapper<>();
        wq.eq(!StringUtils.isEmpty(classId),Specialized::getClassId,classId);
        Specialized one = specializedService.getOne(wq);
        studentVo.setSpecialized(one.getSpecialized());

        return studentVo;
    }

    @Override
    public void importData(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(),StudentVo.class,
                    new StudentListeren(baseMapper,studentCourseMapper,specializedMapper,courseMapper)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}